I.2 (2) How do you access a value in a dictionary?
By its index
By its value
By its key
By its position
Ans: Double click to answer the question
I.3 (3) What is the syntax for adding an element to a dictionary in Python?
my_dict.add(key, value)
my_dict[key] = value
my_dict.append(key, value)
my_dict.extend(key, value)
Ans: Double click to answer the question
I.4 (4) What is a set in Python?
An ordered sequence of elements
A collection of unique elements
A collection of key-value pairs
A sequence of key-value pairs
Ans: Double click to answer the question
I.5 (5) What is the syntax for creating a set in Python?
my_set = {1, 2, 3}
my_set = [1, 2, 3]
my_set = (1, 2, 3)
my_set = {‘1’, ‘2’, ‘3’}
Ans: Double click to answer the question
I.6 (6) What is the difference between a dictionary and a list in Python, and when would you choose to use one over the other?
Ans: Double click to answer the question
The main difference between a dictionary and a list in Python is that a dictionary stores data as key-value pairs, while a list stores data as a sequence of elements. You would choose to use a dictionary over a list when you need to store data in a way that can be easily looked up based on a unique key.
I.7 (7) How do you check if a key exists in a dictionary in Python?
Ans: Double click to answer the question
You can use the in operator to check if a key exists in a dictionary. For example: if key in my_dict:
I.8 (8) How do you create a deep copy of a dictionary in Python?
Ans: Double click to answer the question
You can create a deep copy of a dictionary in Python using the copy() method with the .deepcopy() method from the copy module.
For example:
import copy
new_dict = copy.deepcopy(my_dict)
I.9 (9) Suppose you have a list of student grades, and you want to find the top-performing student and the average grade for each subject. Write a program that does the following:
Create a dictionary called grades that maps subject names to lists of grades for each student. Assume that each student has the same number of grades for each subject.
Create a dictionary called subject_averages that maps subject names to their average grades across all students.
Create a dictionary called student_totals that maps student names to their total grades across all subjects.
Create a variable called top_student that contains the name of the student with the highest total grade.
Print out the top-performing student’s name and total grade, as well as the average grade for each subject.
Top-performing student: Student 2, Total grade: 1342
Math average grade: 89.13333333333334
Science average grade: 87.06666666666666
English average grade: 87.93333333333334
I.10 (10) Write a program that creates a dictionary students that maps student names to their ages. The program should prompt the user to enter the names and ages of at least three students, and then print out the contents of the students dictionary.
sample output:
Enter a student name (or 'q' to quit): Sherry
Enter the student's age: 18
Enter a student name (or 'q' to quit): Money
Enter the student's age: 3
Enter a student name (or 'q' to quit): Steve
Enter the student's age: 30
Enter a student name (or 'q' to quit): q
Student dictionary:
{'Sherry': '18', 'Money': '3', 'Steve': '30'}
# coding your answer herestudents = {}whileTrue: name =input("Enter a student name (or 'q' to quit): ")if name =='q':break age =input("Enter the student's age: ") students[name] = ageprint("Student dictionary:")print(students)